package com.example.controller;

import com.example.pojo.Status;
import com.example.pojo.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@RestController
public class JsonController {

    @Autowired
    UserService userService;

    /**
     * 用户搜索
     * localhost/user/search?username=yy
     * @param username
     * @param model
     * @return
     */
    @RequestMapping("/user/search")
    public List<User> search(String username, Model model) {
        List<User> userList = userService.getUserLikeUsername(username);
        model.addAttribute("list", userList);

        return userList;
    }

    /**
     * 直接返回用户列表，json格式
     * @return
     */
    @RequestMapping("/list")
    public List<User> list() {

        List<User> userList = userService.getUserList();
        return userList;

    }



    /**
     * 登录  直接返回状态
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/user/login")
    public Status login(String username, String password) {

        Status status = new Status();
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            System.out.println("password = " + password);
            status.setInfo("账号密码不能为空");
//            return "账号密码不能为空";

        } else {
            User user = userService.getUserByUsername(username);
            if (user == null) {
                status.setInfo("账号错误");
//                return "账号错误";
            } else if (!password.equals(user.getPassword())) {
                System.out.println("密码错误 " + user.toString());
                status.setInfo("密码错误");
//                return "密码错误";
            } else {
                status.setInfo("登陆成功");
//                return "登陆成功";
            }
        }

        return status;
    }

    /**
     * 注册页面
     * @param user
     * @return
     */
    @RequestMapping("/user/register")
    @ResponseBody
    public Status register(User user) {
        Status status = new Status();

        if (StringUtils.isEmpty(user.getUsername()) ||
                StringUtils.isEmpty(user.getPassword())) {
            status.setInfo("账号密码不能为空");
//            return "账号密码不能为空";
        } else if (user.getUsername().length() > 30 || user.getUsername().length() < 6) {
            status.setInfo("账号长度不合法");
//            return "账号长度不合法";
        } else if (user.getPassword().length() > 18 || user.getPassword().length() < 6) {
            status.setInfo("密码长度不合法");
//            return "密码长度不合法";
        } else if (user.getQq().length() > 12 || user.getQq().length() < 6) {
            status.setInfo("绑定的qq长度长度不合法");
//            return "绑定的qq长度长度不合法";
        } {
            try {
                int i = userService.addUser(user);
                System.out.println("user = " + user);
                System.out.println("注册成功");
                status.setInfo("注册成功,请登录");
//                return "注册成功,请登录";

            } catch (Exception e) {
                status.setInfo("账号已经被注册");
//                return "账号已经被注册";/
            }
        }


        return status;
    }


}
